{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dff582ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "afad19f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "print(a)\n",
    "print(a.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "198af566",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "int8\n"
     ]
    }
   ],
   "source": [
    "b = np.array([1,2,3,4,5],dtype = 'i1')\n",
    "print(b)\n",
    "print(b.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d0209c86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3. 4. 5.]\n",
      "float16\n"
     ]
    }
   ],
   "source": [
    "c = np.array([1,2,3,4,5],dtype = np.float16)\n",
    "print(c)\n",
    "print(c.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "74e8c5fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<__main__.Person object at 0x000001F843371790>\n",
      " <__main__.Person object at 0x000001F84BA7EFA0>]\n",
      "object\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def __init__(self,name,age):\n",
    "        self.age = age\n",
    "        self.name = name\n",
    "\n",
    "d = np.array([Person(\"小米\",20),Person(\"华为\",18)])\n",
    "print(d)\n",
    "print(d.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "58e0af2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[b'a' b'c']\n",
      "|S1\n"
     ]
    }
   ],
   "source": [
    "f = np.array(['a','c'],dtype = 'S')\n",
    "print(f)\n",
    "print(f.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bff4c0c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 'c']\n",
      "<U1\n"
     ]
    }
   ],
   "source": [
    "e = np.array(['a','c'],dtype='U')\n",
    "print(e)\n",
    "print(e.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "23e9f211",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 'c']\n",
      "<U1\n",
      "|S1\n"
     ]
    }
   ],
   "source": [
    "uf = f.astype(\"U\")\n",
    "print(uf)\n",
    "print(uf.dtype)\n",
    "print(f.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ce37ecc",
   "metadata": {},
   "source": [
    "# 总结\n",
    "## 1.为什么Numpy的数组中有这么多的数据类型？\n",
    "    *Numpy本身是基于C语言编写，C语言中本身就有很多数据类型，所以直接引用过来了\n",
    "    *Numpy为了考虑到处理海量数据的性能，针对不同的数据给不同的数据类型，来节省内存空间\n",
    "## 2.Numpy的数组的元素有哪些数据类型\n",
    "## 3.使用ndarray.artype可以修改数组元素的数据类型"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
